using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using limpalex.bbparser.Interfaces;

namespace limpalex.bbparser
{
    public class ReflectionAttributeValidator : IAttributeValidator
    {
        private MethodInfo _validateMethod;
        public bool ValidateAttribute(IParameterList parameters, string name)
        {
            return (bool) _validateMethod.Invoke(null, new object[] {parameters, name});
        }

        public string[] SupportedAttributes { get; private set; }

        public static ReflectionAttributeValidator Create
            (string[] supportedAttributes, string validateMethod, string validateMethodType)
        {
            
            var retval = new ReflectionAttributeValidator {SupportedAttributes = supportedAttributes};

            Type type = Type.GetType(validateMethodType);
            retval._validateMethod = type.GetMethod
                (validateMethod, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, 
                    null, new [] {typeof (IParameterList), typeof (string)}, null);
            return retval;
        }

        private ReflectionAttributeValidator()
        {
            
        }
    }
}
